package a00800658.dbtool.data;

import java.sql.*;

public class DatabaseAccessObject {
	
	private Connection con;
	private DatabaseMetaData dbMetaData;
	private Statement stmt;
	private ResultSet rs;
	private ResultSetMetaData rsMetaData;
	
	public DatabaseAccessObject(){}
	
	public void loadDatabaseDriver(String className) throws ClassNotFoundException{
		Class.forName(className);
	}
	
	public void connect(String url, String userName, String passWord) throws SQLException{
		if (userName == null || passWord == null) 
			con = DriverManager.getConnection(url);
		else
			con = DriverManager.getConnection(url, userName, passWord);
	}
	
	public Connection getConnection(){
		return con;
	}
	
	public void closeConnection() throws SQLException{
		if(con != null)
			con.close();
	}
	
	public DatabaseMetaData getDatabaseMetadata(Connection connection) throws SQLException{
		dbMetaData = connection.getMetaData();
		return dbMetaData;
	}
	
	public void initializeStatement(int resultSetType, int resultSetConcurrency) throws SQLException{
		stmt = con.createStatement(resultSetType, resultSetConcurrency);
	}	 
	
	public Statement getStatement(){
		return stmt;
	}
	
	public void closeStatement() throws SQLException{
		if(stmt != null)
			stmt.close();
	}
	
	public ResultSet getResultSet(){
		return rs;
	}
	
	public void closeResultSet() throws SQLException{
		if(rs != null)
			rs.close();
	}
	
	public ResultSet generateResultSet(String selectQuery) throws SQLException{
		rs = stmt.executeQuery(selectQuery);
		return rs;
	}
	
	public ResultSetMetaData getResultSetMetadata(ResultSet resultSet) throws SQLException{
		rsMetaData = resultSet.getMetaData();
		return rsMetaData;
	}
	
	public int executeUpdateQuery(String updateQuery) throws SQLException{
		return stmt.executeUpdate(updateQuery);
	}
}
